<?php
namespace Swiftx\Component\Bundle;
use Swiftx\Interfaces\Bundle\Kernel as KernelInterfaces;
use Swiftx\Bundle\Console\Controller\Interfaces;

/**
 * 控制台UI框架Bundle模块
 * @package Swiftx\Bundle\Console
 */
abstract class Kernel implements KernelInterfaces{

    /**
     * 是否已引导
     * @var bool
     */
    protected $booted = false;

    /**
     * Composer配置
     * @var array
     */
    protected $composer = null;

    /**
     * 依赖选项
     * @var string[]
     */
    protected $dependents = [];

    /**
     * 所在路径
     * @var string
     */
    protected $rootPath = null;

    /**
     * 获取名称
     * @return string
     * @throws \ErrorException
     */
    public function getName():string{
        if($this->composer != null)
            return $this->composer['name'];
        $composer_path = $this->getRootPath().'/composer.json';
        if(!file_exists($composer_path))
            throw new \ErrorException('找不到模块配置文件：'.$composer_path);
        $this->composer = json_decode(file_get_contents($composer_path), true);
        return $this->composer['name'];
    }

    /**
     * 获取依赖
     * @return string[]
     */
    public function getDependents():array{
        return $this->dependents;
    }

    /**
     * 是否已启动
     * @return bool
     */
    public function hasBooted():bool{
        return $this->booted;
    }

    /**
     * 获取所在目录
     * @return string
     */
    public function getRootPath():string {
        if($this->rootPath) return $this->rootPath;
        $class = new \ReflectionClass(static::class);
        $this->rootPath = dirname(dirname($class->getFileName()));
        return $this->rootPath;
    }

}